import { createRouter, createWebHistory } from 'vue-router';
import HomePage from '../components/HomePage.vue';  // 目标跳转页面
import LoginPage from '../components/LoginPage.vue'; // 登录页面
import Header from '@/components/header.vue';
import Organization from '../components/SystemChildren/Organization.vue'
import UserManagement from '../components/SystemChildren/UserManagement.vue'
import RolePermissions from '../components/SystemChildren/RolePermissions.vue'
import OperationLogs from '../components/SystemChildren/OperationLogs.vue'
import loginLogs from '@/components/SystemChildren/loginLogs.vue';
import changePassword from '@/components/changePassword.vue';
import TowerList from '../components/ResourceChildren/towerList.vue';
import DJI from '@/components/ResourceChildren/DJI.vue';
import TowerType from '@/components/ResourceChildren/towerType.vue';
import Algorithm from '@/components/ResourceChildren/algorithm.vue'
import Task from '@/components/IntelligentChildren/task.vue';
import Trace from '@/components/IntelligentChildren/trace.vue'
import Result from '@/components/IntelligentChildren/result.vue'
import Alarm from '@/components/IntelligentChildren/alarm.vue';
import Flaw from '@/components/IntelligentChildren/flaw.vue';
import NewHomePage from '@/components/NewHomePage.vue';
const routes = [
  // { path: '/newHomePage', name: 'NewHomePage', component: NewHomePage },
  { path: '/', name: 'Login', component: LoginPage },
  {path:'/changePassword' ,name:'ChangePassword' ,component:changePassword },
  {
    path:'/header',
    name:'Header',
    component:Header,
    children:[
      { 
        path: '/home', 
        name: 'Home', 
        component: HomePage, 
        meta: { requiresAuth: true }, // 添加自定义元信息，用于判断是否需要登录
      },
      {
        path: '/newHomePage',
        name: 'NewHomePage',
        component: NewHomePage,
        children:[
          // { 
          //   path: '/newHomePage/changePassword' , 
          //   name :'changePassword', 
          //   component:changePassword },
          { 
            path:'/newHomePage/towerList' ,
            name:'TowerList',
            component:TowerList,
           },
           {
             path:'/newHomePage/DJI',
             name:"DJI",
             component: DJI,
           },
           {
             path:'/newHomePage/towerType',
             name:'TowerType',
             component:TowerType
           },
           {
            path:'/newHomePage/algorithm',
            name:'Algorithm',
            component:Algorithm
          },
          {
            path: '/newHomePage/organization',
            name: 'Organization',
            component: Organization,
          },
          {
            path: '/newHomePage/usermanagement',
            name: 'UserManagement',
            component: UserManagement,
          },
          {
            path: '/newHomePage/rolepermissions',
            name: 'RolePermissions',
            component: RolePermissions,
          },
          {
            path: '/newHomePage/operationLogs',
            name: 'OperationLogs',
            component: OperationLogs,
          },
          {
            path: '/newHomePage/loginLogs',
            name: 'LoginLogs',
            component: loginLogs,
          },
          {
            path:'/newHomePage/task',
            name:'Task',
            component:Task
          },
          {
            path:'/newHomePage/trace',
            name:'Trace',
            component:Trace
          },
          {
            path:'/newHomePage/result',
            name:'Result',
            component:Result
          },
          {
            path:'/newHomePage/alarm',
            name:'Alarm',
            component:Alarm
          },
          {
            path:'/newHomePage/flaw',
            name:'Flaw',
            component:Flaw
          }
        ]
      },  
    ]
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 全局路由守卫
router.beforeEach((to, _from, next) => {
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'; // 检查本地存储中的登录状态

  if (to.meta.requiresAuth && !isLoggedIn) {
    // 如果目标路由需要登录且用户未登录
    alert('请先登录！');
    next('/'); // 跳转到登录页面
  } else {
    next(); // 否则允许导航
  }
});

export default router;
